package westh.ilib.struts.action.KeyWordCloud;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

//import westh.ilib.bean.zhaojie.AcademicMeeting;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.List;

import westh.config.Config;
import westh.ilib.bean.KeyWordCloud.KeyWordCloud;
import westh.log.*;
import westh.config.Config;
/**
 * @author linjia keyWordCloud
 */

public class GetKeyWordCloudAction extends Action {
	private final static Log log = LogFactory
			.getLog(GetKeyWordCloudAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到输入的Json-串
		DynaActionForm f = (DynaActionForm) form;
		log.debug("in GetKeyWordCloudAction");
		// String inputString = f.getString("inputStr");
		// servlet获取界面数据并建立Json

		// 起始时间
		String dateStart = request.getParameter("dateStartyear") + "-"
				+ request.getParameter("dateStartmonth") + "-01 00:00:00";
		// 终止时间
		String dateEnd = request.getParameter("dateEndyear") + "-"
				+ request.getParameter("dateEndmonth") + "-01 00:00:00";
		// 显示几个关键词
		String dispNum = request.getParameter("dispNum");
		// 查询区域（单位小从大学、研究所，大到国家地区、州省）

		// 组建Json
		String area = request.getParameter("area");
		JSONObject jsonData = new JSONObject();
		jsonData.put("queryDateStart", dateStart);
		jsonData.put("queryDateEnd", dateEnd);
		jsonData.put("keywordDispNum", dispNum);
		jsonData.put("area", area);

		JSONObject inJson = new JSONObject();
		inJson.put("methodName", "getQueryLog");
		inJson.put("jsonStr", jsonData);
		String inputStr = inJson.toString();
		log.debug("input = " + inputStr);
		// jSon-String
		String outString = "";
		/************** 获取jSon信息 start ***************************/
		try {
			MuleClient client = new MuleClient();
			// 调用WebService,向后台发送数据
			// UMOMessage umo =
			// client.send("axis:http://166.111.134.22:5181/Test/yyiLibTest?method=rulesControl",inJson,null);
			UMOMessage umo = client.send("axis:http://"+remoteUrl+":9281/Service/ulasService?method=rulesControl",inputStr, null);

			outString = (String) umo.getPayload();
			log.debug(" outString=" + outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// **************获取jSon信息 end***************************/

		// *****存数据*****//
		// YyLog A = new YyLog();
		// A.rulesSave(request, inputStr);
		// ***************//

		String retMsg = "";// 返回结果信息
		String retCode = ""; // 返回代码
		String returnStr = "";// 跳转信息

		// return code and message of keywordcloud function
		String resultCode = "";
		String resultMsg = "";
		log.debug("in action outString=" + outString);
		/**************** 解析输出jSon串 start *********************/
		JSONObject jsonOut = JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("3511")) {
			String result = jsonOut.getString("resultArray");
			// log.debug("into 3511="+result);
			JSONArray resultArry = JSONArray.fromObject(result);
			// log.info("JSONArray="+resultArry.size());
			log.debug("Action JsonArray = " + resultArry.toString());
			// 解析数组
			KeyWordCloud keyWordCloud = new KeyWordCloud();
			List list = new ArrayList();
			//String dirpath = Config.get("keywordcloudpath");
			//String filepath = dirpath + "data.xml";
			//log.debug("!!!!!!!!!!!!!filepath" + filepath);

			//FileWriter fw = new FileWriter(filepath);
			//String command = "";
			//String command_head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rtt version=\"1.0\">";
			//fw.write(command_head);
			// String url="/ilib/KeyWordCloud/KeyWordCloud.html?paramsize=" +
			// resultArry.size();
			ActionForward forward=mapping.findForward("succ");  
			String newPath=forward.getPath()+"?size="+resultArry.size(); 			
			for (int i = 0; i < resultArry.size(); i++) {
				keyWordCloud = new KeyWordCloud();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				keyWordCloud.setKeyWord(jsonOut2.getString("keyWord"));
				keyWordCloud.setKeyWordQueryTimes(jsonOut2
						.getString("queryTimes"));
				list.add(keyWordCloud);
				String keyword = jsonOut2.getString("keyWord");
				int j = i + 1;
				newPath = newPath + "&"+"w"+j+"="+keyword;			
			}
			/**************** 解析输出jSon串 end *********************/
			String command_foot = "</rtt>";
			//command = command + command_foot;
			//fw.write(command, 0, command.length());
			//fw.flush();
			//request.setAttribute("list", list);
			// log.debug(url);
			// return new ActionForward(url);
			//return newForward;
			response.sendRedirect(newPath);
		}
		if (retCode.equals("3512")) {
			resultMsg = "后台没有指定数据";
			request.setAttribute("info", resultMsg);
			returnStr = "null";
		}
		if (retCode.equals("3513")) {
			resultMsg = "关键词云模块执行失败！";
			request.setAttribute("info", resultMsg);
			returnStr = "fail";
		}

		log.debug("in action return =" + returnStr);

		// ActionForward af = new
		// ActionForward("D:\\workspace\863\ilib\WebRoot\ilib\KeyWordCloud\ilib\ilib\haha.html");
		// return af;
		return mapping.findForward(returnStr);
	}

}
